package com.ericsson.mhealth.server;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ericsson.mhealth.server.model.MeasurementAtomicUnit;
import com.google.inject.Singleton;
import com.google.template.soy.SoyFileSet;
import com.google.template.soy.data.SoyListData;
import com.google.template.soy.data.SoyMapData;
import com.google.template.soy.tofu.SoyTofu;

@SuppressWarnings("serial")
@Singleton
public class MeasurementData2 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		
		SoyFileSet sfs = (new SoyFileSet.Builder()).add(new File("templates/MeasurementData2.soy")).build();

	    // Compile the template into a SoyTofu object.
	    // SoyTofu has a render method that can render all the templates in the SoyFileSet.
	    SoyTofu tofu = sfs.compileToJavaObj();

	    // Call the template with no data.
	    String response = tofu.render("com.ericsson.mhealth.server.response",
	    		new SoyMapData("measurementValues", new SoyListData(
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388242490")), new Double(10)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388247490")), new Double(12)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388252490")), new Double(14)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388242490")), new Double(10)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388247490")), new Double(12)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388252490")), new Double(14)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388242490")), new Double(10)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388247490")), new Double(12)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388252490")), new Double(14)),
	    					new MeasurementAtomicUnit(new Date(Long.parseLong("1261388242490")), new Double(10))
    					)),
    		null);
	    out.println(response.replace("&quot;", "\""));
		out.flush();
	}

}
